<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>水平垂直居中</title>
  <style>
    body {
      margin: 0;
      padding: 0 20px;
    }
    section {
      margin: 10px;
      display: inline-block;
      overflow: hidden;
    }
    h4 {
      margin: 0;
      padding: 10px 0;
    }
    .box {
      width: 200px;
      height: 200px;
      border: 1px solid #f00;
    }
    .box-child {
      width: 100px;
      height: 100px;
      background-color: #ff0;
    }
    .box_1, .box_2, .box_3 {
      position: relative;
    }
    .box_1-child {
      position: absolute;
      top: 50%;
      left: 50%;
      margin-top: -50px;
      margin-left: -50px;
    }
    .box_2-child {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
    .box_3-child {
      position: absolute;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      margin: auto;
    }
    .box_4 {
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .box_5 {
      display: grid;
    }
    .box_5-child {
      justify-self: center;
      align-self: center;
      /* 或：margin: auto; */
    }
    .box_6 {
      display: table-cell;
      text-align: center;
      vertical-align: middle;
    }
    .box_6-child {
      display: inline-block; /* 或： margin: auto; */
    }
    .box_7 {
      text-align: center;
    }
    .box_7::after {
      content: "";
      display: inline-block;
      height: 100%;
      vertical-align: middle;
    }
    .box_7 > img {
      vertical-align: middle;
    }
  </style>
</head>
<body>
  <section>
    <h4>定宽高-绝对定位+负margin</h4>
    <div class="box box_1">
      <div class="box-child box_1-child"></div>
    </div>
  </section>
  <section>
    <h4>定宽高-绝对定位+transform</h4>
    <div class="box box_2">
      <div class="box-child box_2-child"></div>
    </div>
  </section>
  <section>
    <h4>定宽高-绝对定位+left/right/top/bottom=0+margin=auto</h4>
    <div class="box box_3">
      <div class="box-child box_3-child"></div>
    </div>
  </section>
  <section>
    <h4>定宽高-flex</h4>
    <div class="box box_4">
      <div class="box-child box_4-child"></div>
    </div>
  </section>
  <section>
    <h4>定宽高-grid</h4>
    <div class="box box_5">
      <div class="box-child box_5-child"></div>
    </div>
  </section>
  <section>
    <h4>定宽高-table-cell + vertical-align + inline-block/margin: auto</h4>
    <div class="box box_6">
      <div class="box-child box_6-child"></div>
    </div>
  </section>
  <section>
    <h4>不定宽高-绝对定位+transform</h4>
    <div class="box">
      参见“定宽高-绝对定位+transform”
    </div>
  </section>
  <section>
    <h4>不定宽高-flex</h4>
    <div class="box">
      参见“定宽高-flex”
    </div>
  </section>
  <section>
    <h4>不定宽高-grid</h4>
    <div class="box">
      参见“定宽高-grid
    </div>
  </section>
  <section>
    <h4>不定宽高-table-cell</h4>
    <div class="box">
      参见“定宽高-table-cell”
    </div>
  </section>
  <section>
    <h4>图片垂直居中-::after</h4>
    <div class="box box_7">
      <img src="https://ss1.baidu.com/70cFfyinKgQFm2e88IuM_a/forum/pic/item/242dd42a2834349b406751a3ceea15ce36d3beb6.jpg">
    </div>
  </section>
</body>
</html>